#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int selectInterval()
{
	int interval [4] = {12,4,2,1};
	int choice=0;
	int n;
	cout<<"Select interval between withdrawals: "<<endl;
	cout<<"1. Monthly: Payment is made every month"<<endl;
	cout<<"2. Quarterly: Payment is made every 3 months"<<endl;
	cout<<"3. Semiannualy: Payment is made every 6 months"<<endl;
	cout<<"4. Yearly: Payment is made every 12 months"<<endl;
	cout<<"Your interval selection is: ";
	cin>>choice;
	choice=choice-1;

	n=interval[choice];

	return n;
}

void calWithdrawal()
{
	double P;
	double i;
	double N;
	int n;
	double PMT;

	cout<<"To calculate payment that the annuity will generate each period."<<endl;
	cout<<"*******************************************************"<<endl;
	cout<<"Please enter your starting principle: $";
	cin>>P;
	n=selectInterval();	 
	cout<<"Please enter the annual growth rate (in %): ";
	cin>>i;
	i=i/(100*n);
	cout<<"Please enter the length of annuity in years: ";
	cin>>N;
	N=N*n;

	PMT=P/((1-pow(1+i,-N))/i);
	cout<<"Your withdrawal amount is: $"<<setprecision(2)<<fixed<<PMT<<endl;
	cout<<"*******************************************************"<<endl;
}

void calStartPrinciple()
{
	double P;
	double i;
	double N;
	int n;
	double PMT;

	cout<<"To calculate the initial amount needed to produce the payment desired."<<endl;
	cout<<"*******************************************************"<<endl;
	cout<<"Please enter your withdrawal amount: $";
	cin>>PMT;
	n=selectInterval();	 
	cout<<"Please enter the annual growth rate (in %): ";
	cin>>i;
	i=i/(100*n);
	cout<<"Please enter the length of annuity in years: ";
	cin>>N;
	N=N*n;

	P=PMT*((1-pow(1+i,-N))/i);
	cout<<"Your starting principle is: $"<<setprecision(2)<<fixed<<P<<endl;
	cout<<"*******************************************************"<<endl;
}

void calYears()
{
	double P;
	double i;
	double N;
	int n;
	double PMT;

	cout<<"To calculate the years needed for the desired return."<<endl;
	cout<<"*******************************************************"<<endl;
	cout<<"Please enter your withdrawal amount: $";
	cin>>PMT;
	cout<<"Please enter your starting principle: $";
	cin>>P;
	n=selectInterval();	 
	cout<<"Please enter the annual growth rate (in %): ";
	cin>>i;
	i=i/(100*n);

	N=((log(PMT)-log(PMT-P*i))/log(1+i))/n;
	cout<<"The years needed is: "<<setprecision(1)<<fixed<<N<<" years"<<endl;
	cout<<"*******************************************************"<<endl;
}	

void AnnuityCalculator()
{
	int choice;
	char exitProgram;
	
	cout<<"*******************************************************"<<endl;
	cout<<"Welcome to the Annuity Calculator. \nProgrammed by RuiXuan Liao."<<endl;
	cout<<"*******************************************************"<<endl;

	while (true)
	{
		cout<<"What do you want to do?"<<endl;
		cout<<"1. Calculate my withdrawal amount."<<endl;
		cout<<"2. Calculate my starting principal."<<endl;
		cout<<"3. Calculate my length of annuity in years."<<endl;	
		cout<<"4. Exit."<<endl;
		cout<<"*******************************************************"<<endl;
		cin>>choice;

		if(choice==4)
		{
			break;
		}

		else if(choice==1)
		{
			calWithdrawal();
			cout<<"Do you want to continue? (y/n)";
			cin>>exitProgram;
			if (exitProgram=='n'||exitProgram=='N')
				break;
		}

		else if(choice==2)
		{
			calStartPrinciple();
			if (exitProgram=='n'||exitProgram=='N')
				break;
		}
		
		else if(choice==3)
		{
			calYears();
			if (exitProgram=='n'||exitProgram=='N')
				break;
		}
	
	}
	system("PAUSE");
	}
